<script setup lang="ts">
import { useGuessList } from '@/composables'
import { useMemberStore } from '@/stores'
import { postDailySignatureAPI, getMyselfDataAPI } from '@/services/home'
import { ref, reactive } from 'vue'
import { onLoad, onPullDownRefresh } from '@dcloudio/uni-app'

// 页面加载
// onLoad(async () => {
//   memberStore = useMemberStore()
// })
// 获取屏幕边界到安全区域距离
const { safeAreaInsets } = uni.getSystemInfoSync()
// 获取会员信息
let memberStore = reactive<any>({})
memberStore = useMemberStore()
const { onScrolltolower } = useGuessList()
const downloadTypes = [
  { type: 1, text: '导学案', icon: 'icon-currency' },
  { type: 2, text: '试题', icon: 'icon-gift' },
  { type: 3, text: '收藏', icon: 'icon-check' },
]
const downloadedTypes = [
  { type: 1, text: '导学案', icon: 'icon-comment' },
  { type: 2, text: '试题', icon: 'icon-gift' },
]
// 发送签到到后端api
const postSignature = async () => {
  const res = await postDailySignatureAPI()
  console.log('签到结果：', res)
  if (typeof res.results === 'object' && res.results !== null) {
    uni.showToast({ icon: 'success', title: '签到成功' })
    memberStore.profile.bank_point = res.results.bank_point
  }
}
// 签到点击事件
const onClickDailySignature = () => {
  postSignature()
}
const getMyselfData = async () => {
  console.log('获取个人信息')
  const res = await getMyselfDataAPI()
  console.log('获取个人信息：', res)
  if (res.code == 200) {
    let bank_point = res.data.results.bank_point
    memberStore.setBankpoint(bank_point)
    uni.showToast({ icon: 'success', title: '个人数据更新成功' })
  } else {
    uni.showToast({ icon: 'error', title: '个人数据更新失败' })
  }
}
// 页面下拉刷新
onPullDownRefresh(async () => {
  console.log('下拉刷新')
  getMyselfData()
  uni.stopPullDownRefresh()
})
// 页面加载
onLoad(async () => {
  // console.log('页面加载')
  getMyselfData()
})
</script>

<template>
  <scroll-view enable-back-to-top @scrolltolower="onScrolltolower" class="viewport" scroll-y>
    <!-- 个人资料 -->
    <view class="profile" :style="{ paddingTop: safeAreaInsets!.top + 'px' }">
      <!-- 情况1：已登录 -->
      <view class="overview" v-if="memberStore.profile">
        <view class="profile-base">
          <navigator url="/pagesMember/profile/profile" hover-class="none">
            <image class="avatar" :src="memberStore.profile.avatar" mode="aspectFill"></image>
          </navigator>
          <view class="meta">
            <view class="nickname">
              {{ memberStore.profile.nickname || memberStore.profile.username }}
            </view>
            <navigator class="extra" url="/pagesMember/profile/profile" hover-class="none">
              <text class="update">更新头像昵称</text>
            </navigator>
          </view>
        </view>
        <view class="bank-point">
          <text class="bank-point-itme">库币:{{ memberStore.profile.bank_point.toFixed(1) }}</text>
          <navigator class="settings" url="/pagesMember/settings/settings" hover-class="none">
            设置
          </navigator>
        </view>

        <!-- 我下载到 -->
        <!-- <view class="download">
          <view class="title">
            我下载的
            <navigator url="/pages/download/download?type=0" open-type="navigate" hover-class="none">
              查看全部下载<text class="icon-right"></text>
            </navigator>
          </view>
          <view class="section">
            <navigator v-for="item in downloadTypes" :key="item.type" class="download-item"
              url="`/pages/download-guide/download-guide?type=${item.type}`" open-type="navigate" hover-class="none">
              <text :class="item.icon" class="download-item-icon" />
              <text class="download-item-text">{{ item.text }}</text>
            </navigator>
          </view>
        </view>
        <view class="download">
          <view class="title">
            被下载的
            <navigator url="/pages/download/download?type=0" open-type="navigate" hover-class="none">
              查看全部被下载<text class="icon-right"></text>
            </navigator>
          </view>
          <view class="section">
           
            <navigator v-for="item in downloadedTypes" :key="item.type" class="download-item"
              url="`/pages/download-guide/download-guide?type=${item.type}`" open-type="navigate" hover-class="none">
              <text :class="item.icon" class="download-item-icon" />
              <text class="download-item-text">{{ item.text }}</text>
            </navigator>
          </view>
        </view> -->
        <view class="list">
          <!-- <navigator url="/pages/my/sponsor" hover-class="none" class="item arrow">
            我要赞助
          </navigator> -->
          <button @click="onClickDailySignature" class="item arrow">每日一签</button>
          <navigator url="/pages/my/income" hover-class="none" class="item arrow"
            v-if="memberStore.profile.is_teacher == true">
            我的收益
          </navigator>
          <navigator url="/pages/my/MyVideo" hover-class="none" class="item arrow"
            v-if="memberStore.profile.is_teacher == true">
            我的微课
          </navigator>
          <navigator url="/pages/my/MyWrongQuestion" hover-class="none" class="item arrow"
            v-if="memberStore.profile.is_teacher == false">
            错题本
          </navigator>
          <navigator url="/pages/my/feedback" hover-class="none" class="item arrow">
            问题反馈
          </navigator>
          <navigator url="/pages/my/contact" hover-class="none" class="item arrow">
            联系我们
          </navigator>
          <navigator url="/pages/my/about" hover-class="none" class="item arrow">
            关于IT题库
          </navigator>
        </view>
      </view>

      <!-- 情况2：未登录 -->
      <view class="overview" v-else>
        <navigator url="/pages/login/login" hover-class="none">
          <image class="avatar gray" mode="aspectFill" src="../../static/images/5.png"></image>
        </navigator>
        <view class="meta">
          <navigator url="/pages/login/login" hover-class="none" class="nickname">
            未登录
          </navigator>
          <view class="extra">
            <text class="tips">点击登录账号</text>
          </view>
        </view>
      </view>

    </view>
  </scroll-view>
</template>

<style lang="scss" scoped>
page {
  height: 100%;
  overflow: hidden;
  background-color: #dadadce7;
}

.download-item-icon {
  font-size: 58rpx;
}

.download-item-text {
  font-size: 28rpx;
}

.download-item {
  display: flex;
  flex-flow: column nowrap;
  align-items: center;
}

.viewport {
  height: 100%;
  background-repeat: no-repeat;
  background-image: url(https://pcapi-xiaotuxian-front-devtest.itheima.net/miniapp/images/center_bg.png);
  background-size: 100% auto;
}

/* 用户信息 */
.profile {
  margin-top: 20rpx;
  position: relative;

  .overview {
    display: flex;
    flex-flow: column nowrap;
  }

  .profile-base {
    display: flex;
    height: 120rpx;
    padding: 0 36rpx;
    color: #fff;
  }

  .bank-point {
    display: flex;
    flex-flow: row nowrap;
    justify-content: space-between;
    align-items: center;
    padding: 0 36rpx;
    color: #fff;
    font-size: 40rpx;
  }

  .bank-point-itme {
    font-size: 28rpx;
    margin: 10rpx;
  }

  .avatar {
    width: 120rpx;
    height: 120rpx;
    border-radius: 50%;
    background-color: #eee;
  }

  .gray {
    filter: grayscale(100%);
  }

  .meta {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: flex-start;
    line-height: 30rpx;
    padding: 10rpx 0;
    margin-left: 10rpx;
  }

  .nickname {
    max-width: 180rpx;
    margin-bottom: 16rpx;
    font-size: 30rpx;

    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .extra {
    display: flex;
    font-size: 20rpx;
  }

  .tips {
    font-size: 22rpx;
  }

  .update {
    padding: 3rpx 10rpx 1rpx;
    color: rgba(255, 255, 255, 0.8);
    border: 1rpx solid rgba(255, 255, 255, 0.8);
    margin-right: 10rpx;
    border-radius: 30rpx;
  }

  .settings {
    //position: absolute;
    //bottom: 0;
    //right: 40rpx;
    font-size: 28rpx;
    color: #fff;
  }
}

/* 我的订单 */
.download {
  position: relative;
  z-index: 99;
  padding: 20rpx;
  margin: 20rpx 10rpx 0;
  background-color: #fff;
  border-radius: 10rpx;
  box-shadow: 0 4rpx 6rpx rgba(240, 240, 240, 0.6);
  border-color: #8a8686;
  border-style: hidden hidden solid none;

  .title {
    display: flex;
    flex-flow: row nowrap;
    justify-content: space-between;
    height: 40rpx;
    line-height: 40rpx;
    font-size: 28rpx;
    color: #1e1e1e;

    .navigator {
      font-size: 24rpx;
      color: #939393;
      float: right;
    }
  }

  .section {
    width: 100%;
    display: flex;
    justify-content: space-between;
    padding: 20rpx 10rpx 5rpx;

    .navigator,
    .contact {
      text-align: center;
      font-size: 24rpx;
      color: #333;

      &::before {
        display: block;
        font-size: 60rpx;
        color: #ff9545;
      }

      &::after {
        border: none;
      }
    }

    .contact {
      padding: 0;
      margin: 0;
      border: 0;
      background-color: transparent;
      line-height: inherit;
    }
  }
}

/* 列表 */
.list {
  padding: 0 10rpx;
  background-color: #fff;
  margin-bottom: 20rpx;
  border-radius: 10rpx;

  .item {
    line-height: 90rpx;
    padding-left: 10rpx;
    font-size: 30rpx;
    color: #333;
    border-top: 1rpx solid #ddd;
    position: relative;
    text-align: left;
    border-radius: 0;
    background-color: #fff;

    &::after {
      width: auto;
      height: auto;
      left: auto;
      border: none;
    }

    &:first-child {
      border: none;
    }

    &::after {
      right: 5rpx;
    }
  }

  .arrow::after {
    content: '\e6c2';
    position: absolute;
    top: 50%;
    color: #ccc;
    font-family: 'erabbit' !important;
    font-size: 32rpx;
    transform: translateY(-50%);
  }
}

/* 猜你喜欢 */
.guess {
  background-color: #f7f7f8;
  margin-top: 20rpx;
}
</style>
